package com.myworld.web;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.myworld.model.DemoObj;

/**
 * @RestController：相当于组合了@Controller和@ResponseBody这两个注解
 *
 * 在使用springboot的@RestController注解时，如果不对@RequestMapping的produces属性进行定义，则默认会返回text/html的Content-Type
 */
@RestController
@RequestMapping("/rest")
public class RestDemoController {
	/**
	 * json数据传输
	 * header:
	 *     Content-Type:application/json
	 * body:
	 * 		{
	 *	      "id":1111,
	 *	      "name":"小强"
	 *	    }
	 * @param obj
	 * @return
	 */
	@RequestMapping(value = "/testJson1", method = { RequestMethod.POST }, produces = {
			MediaType.APPLICATION_JSON_UTF8_VALUE })
	public DemoObj testJson1(@RequestBody(required = true) DemoObj obj) {
		System.out.println(obj.getId()+":"+obj.getName());
		return new DemoObj(obj.getId() + 1, obj.getName() + " Ret");
	}
	
	/**
     * 请求方式：/rest/testJson2?id=1&name=Tim
     *
     * @param obj
     * @return
     */
    @RequestMapping(value = "/testJson2", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public DemoObj testJson2(DemoObj obj) {
        return new DemoObj(obj.getId() + 1, obj.getName() + " Ret");
    }
}
